//index.js
//获取应用实例
const app = getApp()

Page({
      data: {
    lo:'https://xiaochengxu.printeasy.top',
        passwordInputHidden:true,
        inputPassword:'',
        id: '',
        idx:'',
        num: 1,
        goods: [],
        payType: 1,
        modelId: 11,
        payid:'',//订单id
        paymony:'',
        buyerMessage: '',
        // 组件所需的参数
        nvabarData: {
          showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
          title: '确认订单', //导航栏 中间的标题
        },
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
      },

      onLoad(options) {
        let id = options.id;
        let modelId = options.modelId;
        console.log(id,modelId)
        let idx = options.idx;
        let that = this;
        that.mylive()
        let url = app.globalData.URL + 'goods/commodity/' + id;
        let data = {};
        app.wxRequest('GET', url, data, (res) => {
          console.log(res)
          let goods = res
          that.setData({
            id:id,
            idx:idx,
            modelId:modelId,
            goods: goods.data
          })
          console.log(that.data.goods)
        }, (err) => {
          console.log(err.errMsg)
        })
      },
      // 计算价格
      calculateTotal: function () {
        let that = this;
        let goods = that.data.goods;
        let num = that.data.num;
        let paymony = goods.price * that.data.num;
        that.setData({
          paymony: paymony
        })
      },
      // 修改地址
      clitLive(){
        wx.navigateTo({
          url: '/modelB/pages/my/address_manage/address_manage',
        })
      },
      // 地址获取
      mylive: function () {
        let that = this;
        let url = app.globalData.URL + 'goods/address/default';
        let data = {};
        app.wxRequest('GET', url, data, (res) => {
          if(res.name==''){
            wx.showModal({
              title: '请添加收货地址',
              success(){
                wx.navigateTo({
                  url: '/modelB/pages/my/add_live/add_live',
                })
              }
            })
          }else{
            let livs = res
          that.setData({
            livs: livs.data
          })
          console.log(livs)
          }
          
        }, (err) => {
          console.log(err.errMsg)
        })
      },
      // 支付方式
      item_change: function (e) {
        // console.log(e.detail.value)
        let pay_type = e.detail.value;
        this.setData({
          payType: pay_type
        })
      },
      inputPassword(e) {
        let that = this;
        console.log(e)
        //键盘输入的密码 赋值给inputPassword
        let inputPassword = that.data.inputPassword + e.currentTarget.dataset.key;
        that.setData({
          inputPassword: inputPassword
        })
        console.log(that.data.inputPassword)
        //当输入密码完成时 提交密码并给个提示
        if (that.data.inputPassword.length == 6) {
          wx.showLoading({
            title: '支付中',
          })
          let url = app.globalData.URL + 'goods/commodity/order/pay?id=' + that.data.payid + '&payPassword=' + that.data.inputPassword;
          let data = {}
          app.wxRequest('POST', url, data, (res1) => {
            wx.hideLoading();
            if (res1.code == 200) {
              // 支付成功
              that.passwordInputHidden();
              wx.showModal({
                title: '支付成功',
                icon: 'none'
              })
              wx.switchTab({
                url: '/pages/index/index'
              });
            }else if (res1.code == 500) {
              //支付密码错误
              wx.showModal({
                title: "提示",
                content: "余额不足",
              })
              that.setData({
                inputPassword: ''
              });
            } else if (res1.code == 502) {
              //支付密码错误
              wx.showModal({
                title: "提示",
                content: "输入密码错误",
              })
              that.setData({
                inputPassword: ''
              });
            } else if (res1.code == 501) {
              // 未设置支付密码
              wx.showModal({
                title: "提示",
                content: "未设置支付密码",
              })
              that.passwordInputHidden()
              wx.navigateTo({
                url: '/pages/my/setPassword/setPassword'
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        }
    
      },
      passwordInputHidden() {
        this.setData({
          passwordInputHidden: !this.data.passwordInputHidden //取反 打开关闭小键盘
        });
        this.setData({
          inputPassword: ''
        });
      },
      //删除输入错误的密码
      clear() {
        var index = this.data.inputPassword.length;
        if (index > 0) {
          var inputPassword = this.data.inputPassword.substr(0, index - 1);
          this.setData({
            inputPassword: inputPassword
          });
        }
      },
      mycoupon: function () {
        wx.navigateTo({
          url: '/pages/my/my_coupon/my_coupon',
          success: function (res) {}, 
          fail: function (res) {},
          complete: function (res) {},
        })
      },
      select_dayin: function (e) {
        let printType = e.detail.value;
        this.setData({
          printType: printType
        })
      },
      select_zhifu: function (e) {
        let pay_type = e.detail.value;
        this.setData({
          pay_type: pay_type
        })
      },
      // 留言
      buyerMessage: function (e) {
        this.setData({
          buyerMessage: e.detail.value
        })
      },
      // 商品加
      addtap: function (e) {
        var index = e.target.dataset.index;
        var num = this.data.num;
        let count = this.data.goods.count;
        if (num >= count) {
          return;
        } else {
          num++;
          this.setData({
            num: num
          })
          this.calculateTotal();
        }
      },
      // 用户-
      subtracttap: function (e) {
        var index = e.target.dataset.index;
        var index = e.target.dataset.index;
        var num = this.data.num;
        if (num <= 1) {
          return;
        } else {
          num--;
          this.setData({
            num: num
          });
          this.calculateTotal();
        }
      },

      // 提交
      submitpic: function () {
        let that = this;
        let addressId = that.data.livs.id;
        let buyerMessage = that.data.buyerMessage;
        let commodityId = that.data.goods.id;
        let count = that.data.num;
        let payType = that.data.payType;
          let modelId = that.data.modelId;
          let orderCommodityList = [{
            commodityId,count, modelId
          }]
          let paymony = that.data.paymony; 
          console.log("地址id:", addressId, "用户留言:", buyerMessage, "商品id:", commodityId, "商品数量:", count, "付款方式:", payType,modelId)
          let url = app.globalData.URL + 'goods/commodity/order';
          let data = JSON.stringify({
            addressId: addressId,
            buyerMessage: buyerMessage,
            orderCommodityList: orderCommodityList,
            payType: payType,
          });
          // 订单创建,调支付
          app.wxRequest('POST', url, data, (res) => {
            console.log(res)
            if(res.code!=200){
              wx.showModal({
                title:res.msg
              })
            }else{
              if (payType == 0) {
                that.setData({
                  payid:res.data.id
                })
                that.passwordInputHidden();
              } else if (payType == 1) {
              let wxzf = res.data;
                wx.requestPayment({
                  timeStamp:wxzf.timeStamp,
                  nonceStr: wxzf.nonceStr,
                  package: wxzf.package,
                  signType: wxzf.signType,
                  paySign: wxzf.paySign,
                  success (res) {
                    console.log('支付成功')
                  },
                  fail (res) { }
                })
              }
            }
           

          })
        }
      })